<?php

class Product extends SiteTree {

    private $currency = array (
        'EUR'=>'Euro',
        'AUD'=>'Australian Dollar',
        'CAD'=>'Canadian Dollar',
        'CZK'=>'Czech Koruna',
        'DKK'=>'Danish Krone',        
        'HKD'=>'Hong Kong Dollar',
        'HUF'=>'Hungarian Forint',
        'ILS'=>'Israeli New Sheqel',
        'JPY'=>'Japanese Yen',
        'MXN'=>'Mexican Peso',
        'NOK'=>'Norwegian Krone',
        'NZD'=>'New Zealand Dollar',
        'PHP'=>'Philippine Peso',
        'PLN'=>'Polish Zloty',
        'GBP'=>'Pound Sterling',
        'SGD'=>'Singapore Dollar',
        'SEK'=>'Swedish Krona',
        'CHF'=>'Swiss Franc',
        'TWD'=>'Taiwan New Dollar',
        'THB'=>'Thai Baht',
        'USD'=>'U.S. Dollar'
    );

    static $singular_name = 'Product';
    static $plural_name = 'Products';

    static $allowed_children = array("none");

    static $default_parent = "ProductCategory";
    
    static $can_be_root = false;

    static $db = array(
        'ItemID' => 'Varchar(255)',
        'ItemCurrency' => "Enum('AUD,CAD,CZK,DKK,EUR,HKD,HUF,ILS,JPY,MXN,NOK,NZD,PHP,PLN,GBP,SGD,SEK,CHF,TWD,THB,USD,','EUR')",
        'ItemPrice' => 'Currency',
        'ItemShipping' => 'Currency',
        'ItemShipping2' => 'Currency',
        'ItemNoShip' => 'Boolean'
    );

    static $many_many = array(
        'ItemOptions' => 'ProductOption'
    );

    static $icon = array("silverstripe-payvment/images/cms/icons/product", "file");

    function getCMSFields() {
      $fields = parent::getCMSFields();

      $fields->renameField('Title', 'Product name');
      $fields->renameField('Content', 'Product describtion');
 
      $optionsTablefield = new ManyManyComplexTableField(
         $this,
         'ItemOptions',
         'ProductOption',
         array(
	    'Title' => 'Title'
         ),
         'getCMSFields_forPopup'
      );
      $optionsTablefield->setAddTitle( 'a ProductOption' );

      $fields->addFieldToTab( 'Root.Content.ProductOptions', $optionsTablefield );

      $fields->addFieldToTab('Root.Content.ProductDetails', new TextField('ItemID', 'Unique product ID'));
      $fields->addFieldToTab('Root.Content.ProductDetails', new DropdownField('ItemCurrency', 'Currency', $this->currency));
      $fields->addFieldToTab('Root.Content.ProductDetails', new CurrencyField('ItemPrice','Product price'));
      $fields->addFieldToTab('Root.Content.ProductDetails', new CurrencyField('ItemShipping','Shipping cost for 1st product of this type in the cart'));
      $fields->addFieldToTab('Root.Content.ProductDetails', new CurrencyField('ItemShipping2','Shipping cost for additional products of this type in the cart'));

      return $fields;
   }
}

class Product_Controller extends ContentController {

    private $service = null;

    static $allowed_actions = array(
        'addProduct' => true,
        'updateProduct' => true,
        'deleteProduct' => true,
    );

    function init() {
        parent::init();
        $this->service = new PayvmentService($this->ItemCurrency);
    }

    function addProduct() {
        //$this->getRequest()->getVar($name)
        $service->addStoreItem($this->dataRecord, 1);
    }

    function updateProduct() {
        $service->updateStoreItem($this->dataRecord);
    }
    
    function deleteProduct() {
        $service->deleteStoreItem($this->dataRecord);
    }
}